<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row style="margin-left: -16px" :gutter="20">
            <a-col :span="6">
              <a-form-item label="公司名称">
                <a-input
                  v-model="queryParam.companyName"
                  placeholder="请输入公司名称"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="联系人">
                <a-input
                  v-model="queryParam.lianxiUser"
                  placeholder="请输入联系人"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-company-add-form
        v-if="showAddModal"
        ref="bizCompanyAddForm"
        :smsIsopenOptions="smsIsopenOptions"
        :ossIsopenOptions="ossIsopenOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 充值-->
      <biz-company-rechange-form
        v-if="showRechangeModal"
        ref="bizCompanyRechangeForm"
        :smsIsopenOptions="smsIsopenOptions"
        :ossIsopenOptions="ossIsopenOptions"
        @ok="getList"
        @close="showRechangeModal = false"
      />
      <!-- 编辑 -->
      <biz-company-edit-form
        v-if="showEditModal"
        ref="bizCompanyEditForm"
        :smsIsopenOptions="smsIsopenOptions"
        :ossIsopenOptions="ossIsopenOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="公司信息"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
          pageSizeOptions: pageSizeOptions,
        }"
        tableKey="base-bizCompany-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizCompanyList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['biz:bizCompany:add']"
          >
            <a-icon type="plus" />新增
          </a-button>
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizCompany:export']"
          >
            <a-icon type="download" />导出
          </a-button>
          <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizCompany:remove']"
          >
            <a-icon type="delete" />删除
          </a-button>
        </div>
        <span slot="smsIsopen" slot-scope="{ text, record }">
          <a-tag color="#2db7f5" v-if="text != '20'">
            {{ smsIsopenFormat(record) }}</a-tag
          >
          <a-tag color="#87d068" v-else> {{ smsIsopenFormat(record) }} </a-tag>
        </span>
        <span slot="ossIsopen" slot-scope="{ text, record }">
          <a-tag color="#2db7f5" v-if="text != '20'">
            {{ ossIsopenFormat(record) }}</a-tag
          >
          <a-tag color="#87d068" v-else> {{ ossIsopenFormat(record) }} </a-tag>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
        <span slot="balance" slot-scope="{ text }">
          <a style="font-size: 14px; color: #87d068; font-weight: 500">
            ¥&nbsp;{{ parseFloat(text ? text : 0).toFixed(2) }}</a
          >
        </span>

        <span slot="operation" slot-scope="{ record }">
          <a @click="handleUpdate(record)" v-hasPermi="['biz:bizCompany:edit']">
            修改
          </a>
          <a-divider type="vertical" v-hasPermi="['biz:bizCompany:remove']" />
          <a
            @click="handleDelete(record)"
            v-hasPermi="['biz:bizCompany:remove']"
          >
            删除
          </a>
          <!-- <a-divider type="vertical" v-hasPermi="['biz:bizCompany:rechange']" />
          <a
            @click="handleRechange(record)"
            v-hasPermi="['biz:bizCompany:rechange']"
          >
            充值
          </a> -->
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizCompany,
  delBizCompany,
  exportBizCompany,
} from "@/api/biz/bizCompany";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizCompanyAddForm from "@/views/biz/company/modules/BizCompanyAddForm";
import BizCompanyEditForm from "@/views/biz/company/modules/BizCompanyEditForm";
import BizCompanyRechangeForm from "@/views/biz/company/modules/BizCompanyRechangeForm";
export default {
  name: "BizCompany",
  components: {
    AdvanceTable,
    BizCompanyAddForm,
    BizCompanyEditForm,
    BizCompanyRechangeForm,
  },
  data() {
    return {
      pageSizeOptions: ["10", "20", "30", "40", "50", "100", "200", "500"],
      showAddModal: false,
      showEditModal: false,
      showRechangeModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 公司信息表格数据
      bizCompanyList: [],
      // 短信通知字典
      smsIsopenOptions: [],
      // OSS文件存储字典
      ossIsopenOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,

        lianxiUser: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "55px",
          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "公司KEY",
          dataIndex: "id",
          ellipsis: true,
          align: "center",
        },
        {
          title: "公司名称",
          dataIndex: "companyName",
          ellipsis: true,
          align: "center",
          width: "220px",
        },
        {
          title: "联系人",
          dataIndex: "lianxiUser",
          ellipsis: true,
          align: "center",
          width: "180px",
        },
        {
          title: "联系电话",
          dataIndex: "lianxiPhone",
          ellipsis: true,
          align: "center",
          width: "180px",
        },

        {
          title: "创建时间",
          dataIndex: "createTime",
          ellipsis: true,
          align: "center",
          width: "180px",
        },
        {
          title: "进线群机器人地址",
          dataIndex: "lianxiAddress",
          ellipsis: true,
          align: "center",
        },
        {
          title: "沟通群机器人地址",
          dataIndex: "remark",
          ellipsis: true,
          align: "center",
        },
        // {
        //   title: "短信通知",
        //   dataIndex: "smsIsopen",
        //   scopedSlots: { customRender: "smsIsopen" },
        //   align: "center",
        //   width: "150px",
        // },
        // {
        //   title: "OSS文件存储",
        //   dataIndex: "ossIsopen",
        //   scopedSlots: { customRender: "ossIsopen" },
        //   align: "center",
        //   width: "150px",
        // },
        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "160px",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then((response) => {
      this.smsIsopenOptions = response.data;
    });
    this.getDicts("is_active").then((response) => {
      this.ossIsopenOptions = response.data;
    });
  },
  methods: {
    /** 查询公司信息列表 */
    getList() {
      this.loading = true;
      listBizCompany(this.queryParam).then((response) => {
        this.bizCompanyList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 短信通知字典翻译
    smsIsopenFormat(row) {
      if (row.smsIsopen) {
        return this.selectDictLabel(this.smsIsopenOptions, row.smsIsopen);
      } else {
        return "";
      }
    },
    // OSS文件存储字典翻译
    ossIsopenFormat(row) {
      if (row.ossIsopen) {
        return this.selectDictLabel(this.ossIsopenOptions, row.ossIsopen);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,

        lianxiUser: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizCompanyAddForm.handleAdd());
    },
    handleRechange(record) {
      this.showRechangeModal = true;
      this.$nextTick(() =>
        this.$refs.bizCompanyRechangeForm.handleRechange(record)
      );
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizCompanyEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizCompanyIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizCompany(bizCompanyIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizCompany(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
